<?php
/**
 * 文章逻辑类
 * @author zengnz
 *
 */
class ArticleLogic extends CommonModel
{
	// 文章子类别
	const SUB_CATEGORY = 'SUB_CATEGORY_';
	// 所有类别
	const ALL_CATEGORY = 'ALL_CATEGORY';
	
	
	// 缓存模型类
	private $_CacheModel = '';
	
	public function __construct() {
		$this -> _CacheModel = D('Cache', 'Service');
		$this -> _CacheModel -> init();
	}
	
	/**
	 * 添加文章
	 */
	public function addArticle() {
		
		
	}
	
	/**
	 * 获取子类别
	 */
	public function getSubCategory($tbName) {
		$key = self::SUB_CATEGORY.$tbName;
		
		$categoryList = $this -> _CacheModel -> get($key);
		
		if (!$categoryList) {
			//获取相关类别
			$categoryList = D('ArticleCategory')
						-> field('*')
						-> where('pid != 0 AND `table` = "article_' . $tbName . '"'  )
						-> select();
			
			$this -> _CacheModel -> set($key, $categoryList);
			$this -> _CacheModel -> saveKeys($key, 'common');
		}
		
		return $categoryList;
	}
	
	/**
	 * 获取所有类别
	 */
	public function getAllCategory() {
		$key = self::ALL_CATEGORY;
		$categoryList = $this -> _CacheModel -> get($key);
		
		if (!$categoryList) {
			$categoryList = D('ArticleCategory')
						-> field('*')
						-> where('id>0')
						-> select();
			
			$this -> _CacheModel -> set($key, $categoryList);
			$this -> _CacheModel -> saveKeys($key, 'common');
		}
		
		return $categoryList;
	}
	
}